데이터 처리 및 가시화#

강좌: 수치해석 프로젝트

주요 패키지#

  • matplotlib 패키지 : 2차원 그래픽에서 널리 활용되는 파이썬 패키지

  • pandas 패키지 : 데이터 분석 라이브러리

참고

matplotlib 패키지#

불러오기#

%matplotlib inline
from matplotlib import pyplot as plt

plt.style.use('ggplot')
plt.rcParams['figure.dpi'] = 150

Sine / Cosine 그래프 그리기 예제#

  • 각도 단위는 Radian 임

    • deg2rad, rad2deg 로 변환

  • \((-\pi, \pi)\) 사이에서 그래프 그리기

import numpy as np

theta = 90
np.cos(np.deg2rad(theta))
6.123233995736766e-17
# 256개 간격으로 x 나눔
X = np.linspace(-np.pi, np.pi, 256)

# Cosine / Sine 함수
C, S = np.cos(X), np.sin(X)
# Plot cosine with a blue continuous line of width 1 (pixels)
plt.plot(X, C, color="blue", linewidth=1.0, linestyle="-")

# Plot sine with a green continuous line of width 1 (pixels)
plt.plot(X, S, color="green", linewidth=1.0, linestyle="-")

# Label 추가
plt.xlabel(r"$\theta$")
plt.ylabel('Value')

# Tick 추가
plt.xticks([-np.pi, 0, np.pi], [r"-$\pi$", 0, r"$\pi$"])

# Legend 추가
plt.legend(['Cosine', 'Sine'])
<matplotlib.legend.Legend at 0x7f7dc71e59d0>
../_images/3ead26975049ee62a004c956a995135a49a34dc2e7843fdba96bb23f95681f41.png

pandas 패키지#

불러오기#

  • import pandas as pd 로 불러온다

자료 구조#

# Pandas 로드
import pandas as pd

Space X 비행 자료 분석#

# CRS9 자료 읽기
crs9 = pd.read_json('https://raw.githubusercontent.com/shahar603/Telemetry-Data/master/SpaceX%20CRS-9/JSON/analysed.json')
# 데이터 자료 크기
print(crs9.shape)

# 데이터 첫부분
crs9.head()
(440, 9)
time velocity altitude velocity_y velocity_x acceleration downrange_distance angle q
0 0 0.000 0.000 0.000 0.0 10.502 0.0 90.0 0.000000
1 1 2.832 0.002 2.476 0.0 11.001 0.0 90.0 4.911422
2 2 3.832 0.005 4.202 0.0 11.508 0.0 90.0 8.988008
3 3 5.271 0.009 6.162 0.0 12.002 0.0 90.0 17.000633
4 4 8.038 0.016 8.490 0.0 12.402 0.0 90.0 39.516119
# 각 컬럼 이름 출력
print(crs9.columns)

# 각 컬럼 특성
crs9.describe()
Index(['time', 'velocity', 'altitude', 'velocity_y', 'velocity_x',
       'acceleration', 'downrange_distance', 'angle', 'q'],
      dtype='object')
time velocity altitude velocity_y velocity_x acceleration downrange_distance angle q
count 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000 440.000000
mean 219.500000 1794.012536 116.162252 481.673602 1634.273132 14.367682 204.791648 34.702305 3215.724452
std 127.161315 1145.939317 78.907506 332.028161 1232.547816 6.337356 211.012820 29.634832 6840.746659
min 0.000000 0.000000 0.000000 -8.026000 -0.897000 0.095000 -0.003000 -0.109000 0.000000
25% 109.750000 835.914250 29.638250 191.436000 473.329250 9.898750 11.621750 7.779000 0.000000
50% 219.500000 1700.613000 133.024500 430.599000 1508.775000 13.415500 132.917500 27.463500 0.000000
75% 329.250000 2578.719250 194.122750 756.319000 2554.734750 16.418000 351.714250 55.487500 364.876881
max 439.000000 4256.990000 211.854000 1140.251000 4256.990000 34.624000 716.952000 90.000000 22634.274976
# 'time' 컬럼을 index 로 설정
crs9 = crs9.set_index('time')

crs9.head()
velocity altitude velocity_y velocity_x acceleration downrange_distance angle q
time
0 0.000 0.000 0.000 0.0 10.502 0.0 90.0 0.000000
1 2.832 0.002 2.476 0.0 11.001 0.0 90.0 4.911422
2 3.832 0.005 4.202 0.0 11.508 0.0 90.0 8.988008
3 5.271 0.009 6.162 0.0 12.002 0.0 90.0 17.000633
4 8.038 0.016 8.490 0.0 12.402 0.0 90.0 39.516119
# 주요 자료 가시화
crs9['velocity'].plot()
plt.ylabel('Velocity (m/s)')
Text(0, 0.5, 'Velocity (m/s)')
../_images/bcfa2f128003b791527491e67ec3b4f572400c7a47aef629f048d2b5fed09d05.png
crs9['acceleration'].plot()
plt.ylabel(r"Acceleration ($m/s^2$)")
Text(0, 0.5, 'Acceleration ($m/s^2$)')
../_images/e0d9e0c41d3e339c3d0ff75aec1e9f21e57cc020f3ef196647a4b03f0e4d7173.png
crs9['angle'].plot()
plt.ylabel(r"Angle ($^\circ$)")
Text(0, 0.5, 'Angle ($^\\circ$)')
../_images/a160393c4f3c9f4594a45f9c05494aa0ec498d5617b46de8e3dc41705ff45f9f.png
# 여러 그래프 가시화
fig, ax = plt.subplots()
crs9['velocity_x'].plot(ax=ax)
crs9['velocity_y'].plot(ax=ax)

ax.set_ylabel('Velocity (m/s)')
ax.legend(['Velocity (X-dir)', 'Velocity (Y-dir)'])
<matplotlib.legend.Legend at 0x7f7dbb8c0190>
../_images/3ee2ae26fd1cdf5a42f918d197dd2d961489bdd4e491a33994e3ab3b7057e000.png
# 고도별 동압 가시화
plt.plot(crs9['q'], crs9['altitude'])
plt.xlabel(r'Dynamic Pressure ($kN/m^2$)')
plt.ylabel('Altitude (km)')
plt.ylim(-5, 45)
(-5.0, 45.0)
../_images/fe8e7eb9784c816d3ce6bf449c76047db0e3d397045a626a01b056a6c574103a.png
# 최대 동압 지점 찾기
t_qmax = crs9['q'].idxmax()
crs9.loc[t_qmax]
velocity                315.143000
altitude                  9.189000
velocity_y              302.010000
velocity_x               89.864000
acceleration             15.194000
downrange_distance        1.094000
angle                    73.240000
q                     22634.274976
Name: 66, dtype: float64
crs9['altitude'].plot()
plt.ylabel("Altitude (km)")

# Draw Max Q line
plt.axvline(t_qmax, linestyle='--', color='gray')
plt.text(t_qmax, 100,'Max-Q', rotation=-90)
Text(66, 100, 'Max-Q')
../_images/bd914b78f6e9858ed88bca090ead01adc6d27191691a27156a49dfb7ae73c4bb.png

데이터 분석#

  • 대기 밀도 계산

    • \(q = \frac{1}{2} \rho V^2\) , 그러므로 \(\rho = \sqrt{\frac{2q}{V^2}}\)

# 밀도 계산
crs9['rho'] = np.sqrt(2*crs9['q'] / crs9['velocity']**2)
# 고도별 밀도 가시화
plt.plot(crs9['rho'], crs9['altitude'])
plt.xlabel(r'Density ($kg/m^3$)')
plt.ylabel('Altitude (km)')
plt.ylim(-5, 45)
(-5.0, 45.0)
../_images/9c1652f66ca532d5b875b7eb388694d6b9740954db041c212fa49827b76ccfa6.png